package com.zx.onlineeducation.service.oss.controller;

import com.zx.onlineeducation.common.util.R;
import com.zx.onlineeducation.service.oss.service.OssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * OSS 前端控制器
 *
 * @author 郑雪
 * @since 2022-01-11
 */
@Api(tags = "图片上传")
@RestController
@RequestMapping("/oss")
@CrossOrigin // 解决跨域
public class OssController {

    /**
     * OSS服务接口
     */
    @Autowired
    private OssService ossService;

    @ApiOperation("上传文件")
    @PostMapping("/upload/{folder}")
    public R uploadOssFile(
            @ApiParam(value = "上传的文件", required = true)
            MultipartFile file,
            @ApiParam(value = "目标文件夹", required = true)
            @PathVariable("folder") String folder) {
        // 如果上传文件不是空
        if(!file.isEmpty()) {
            // 上传文件到OSS服务器
            String url = ossService.uploadFile(file, folder);
            // 返回成功
            return R.ok().data("url", url);
        } else {
            // 返回失败
            return R.error().message("上传文件不能为空！");
        }
    }
}
